package com.sl.au.service.impl;

import java.util.Date;
import java.util.List;
import java.util.Map;

import org.activiti.engine.runtime.ProcessInstance;
import org.apache.catalina.connector.Request;
import org.apache.poi.util.StringUtil;
import org.aspectj.lang.annotation.After;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import com.sl.au.entity.AfterSaleTicket;
import com.sl.au.entity.AfterSaleTicketAdvice;
import com.sl.au.entity.Contract;
import com.sl.au.repository.AfterSaleTicketAdviceRepository;
import com.sl.au.repository.AfterSaleTicketBackRepository;
import com.sl.au.repository.AfterSaleTicketRepository;
import com.sl.au.service.AfterSaleInHouseService;
import com.sl.au.service.AfterSaleOutHouseService;
import com.sl.au.service.AfterSaleTicketService;
import com.sl.au.service.AuditNodeService;

@Service("AfterSaleTicketService")
public class AfterSaleTicketServiceImpl extends BaseServiceImpl implements AfterSaleTicketService {

	@Autowired
	AfterSaleTicketRepository afterSaleTicketRepository;
	@Autowired
	AfterSaleTicketBackRepository afterSaleTicketBackRepository;
	@Autowired
	AfterSaleTicketAdviceRepository afterSaleTicketAdviceRepository;
	@Autowired
	AuditNodeService auditNodeService;
	@Autowired
	AfterSaleOutHouseService afterSaleOutHouseService;
	@Autowired
	AfterSaleInHouseService afterSaleInHouseService;

	@Override
	public AfterSaleTicketRepository getTicketRepository() {
		return this.afterSaleTicketRepository;
	}

	@Override
	public AfterSaleTicketBackRepository getBackRepository() {
		return this.afterSaleTicketBackRepository;
	}

	@Override
	public AfterSaleTicketAdviceRepository getAdviceRepository() {
		return this.afterSaleTicketAdviceRepository;
	}

	@Override
	public List<AfterSaleTicket> getAfterSaleTicket(int start, int length, Map<String, String> condition,
			String orderName, String dir) {
		Page<AfterSaleTicket> pages = null;
		Pageable pageable = new PageRequest(start / length, length, Sort.Direction.DESC, "createTime");
		if (condition.get("dataFilter").equals("所有"))
			pages = afterSaleTicketRepository.findAllTicketByKey(condition.get("key"), condition.get("roleName"),condition.get("isSumbit"),
					condition.get("afterSaleType"), pageable);
		else if (condition.get("dataFilter").equals("本部门"))
			pages = afterSaleTicketRepository.findSuperiorPage(condition.get("key"), condition.get("superior"),
					condition.get("userId"),condition.get("isSumbit"), condition.get("afterSaleType"), pageable);
		else if (condition.get("dataFilter").equals("本片区"))
			pages = afterSaleTicketRepository.finAreaPage(condition.get("key"), condition.get("areaId"),
					condition.get("userId"), condition.get("isSumbit"), condition.get("afterSaleType"), pageable);
		else if (condition.get("dataFilter").equals("本人"))
			pages = afterSaleTicketRepository.findTicketBykey(condition.get("key"), condition.get("userId"), condition.get("isSumbit"),
					condition.get("afterSaleType"), pageable);
		return pages.getContent();
	}

	@Override
	public long getTicketCount(Map<String, String> condition) {
		long count = 0;
		
		if (condition.get("dataFilter").equals("所有"))
			count = afterSaleTicketRepository.getAllCount(condition.get("key"), condition.get("roleName"),condition.get("isSumbit"),
					condition.get("afterSaleType"));
		else if (condition.get("dataFilter").equals("本部门"))
			count = afterSaleTicketRepository.getSuperiorCount(condition.get("key"), condition.get("superior"),
					condition.get("userId"),condition.get("isSumbit"),condition.get("afterSaleType"));
		else if (condition.get("dataFilter").equals("本片区"))
			count = afterSaleTicketRepository.getAreaCount(condition.get("key"), condition.get("areaId"),
					condition.get("userId"),condition.get("isSumbit"),condition.get("afterSaleType"));
		else
			count = afterSaleTicketRepository.getCountBykey(condition.get("key"), condition.get("userId"),condition.get("isSumbit"),condition.get("afterSaleType"));
		return count;
	}

	@Override
	public List<Contract> getContract(int start, int length, String key, String orderName, String dir) {
		Page<Contract> pages = null;
		Pageable pageable = new PageRequest(start / length, length, Sort.Direction.DESC, "createTime");
		pages = afterSaleTicketRepository.getContracts(key, pageable);
		return pages.getContent();
	}

	@Override
	public void saveProcessStart(String ticketId, ProcessInstance pInst) {
		AfterSaleTicketAdvice ticket = afterSaleOutHouseService.getRepository().getTicketTab3Data(ticketId).get(0);
		if (null == pInst || null == ticket)
			return;
		// 如果是重新提交审批，删除原来的审批节点
		auditNodeService.getRepository().deleteByPInstId(ticket.getProcessInstId());
		ticket.setProcessInstId(pInst.getId());
		ticket.setAuditState("已提交");
		ticket.setStartDate(new Date());
		afterSaleTicketAdviceRepository.save(ticket);
		this.setAfterSaleTicketState(ticketId, "已提交");

	}

	@Override
	public void setAfterSaleTicketState(String ticketId, String auditState) {
		AfterSaleTicket one = afterSaleInHouseService.getAfterSaleTicketRepository().getOne(ticketId);
		if (null != one) {
			one.setTicketState(auditState);
			afterSaleInHouseService.getAfterSaleTicketRepository().save(one);
		}

	}

	@Override
	public AfterSaleTicketAdvice getTicketAdviceByTicketId(String ticketId) {

		return this.getAdviceRepository().findByTicketId(ticketId);
	}

	@Override
	public List<AfterSaleTicket> geSaleTicketpage(int start, int length, String key, String customerId, String orderName,
			String dir) {
		// TODO Auto-generated method stub
		Page<AfterSaleTicket>pages=null;
		Pageable pageable=new PageRequest(start / length, length, Sort.Direction.DESC, "createTime");
		pages=afterSaleTicketRepository.findCustomerAfterSaleInfo(key, customerId,pageable);
		return pages.getContent();
	}
	public long getTicketCount(String key,String customerId){
		long count=0;
		count=afterSaleTicketRepository.getCount(key, customerId);
		return count;
		
	}
//	@Override
//	public List<AfterSaleTicket> geSaleTicketpage(int start, int length, String key, String unitName, String orderName,
//			String dir) {
//		// TODO Auto-generated method stub
//		Page<AfterSaleTicket>pages=null;
//		Pageable pageable=new PageRequest(start / length, length, Sort.Direction.DESC, "createTime");
//		pages=afterSaleTicketRepository.findCustomerAfterSaleInfo(key, unitName, pageable);
//		return pages.getContent();
//	}
//	public long getTicketCount(String key,String unitName){
//		long count=0;
//		count=afterSaleTicketRepository.getCount(key, unitName);
//		return count;
//		
//	}

	
}
